import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

/**
 * @author alek
 *
 */
 
public class DefaultInterfaceData extends AbstractTableModel implements InterfaceData  {
	private static final long serialVersionUID = 1L;
	Object mData[][];
	Dimension mSize;
	CII_Tree_Vectors mInterface;
	
	DefaultInterfaceData()
	{
		super();
		mSize=new Dimension(0,0);
		mData=new Integer[0][0];
	}
	
	public int getCellSize() {
		if(mInterface!=null) return Integer.parseInt(mInterface.get(3));
		else return -1;
	}

	public Vector<Object> getData() {
		return getData(0,0,mSize.width-1,mSize.height-1);
	}

	public Vector<Object> getData(int x1, int y1, int x2, int y2) {
		Vector<Object> tmp=new Vector<Object>();
		for(int y=y1;y<=y2;y++)
			for(int x=x1;x<=x2;x++)
			{
				tmp.add(mData[x][y]);
			}
		return tmp;
	}

	public Dimension getSize() {
		return mSize;
	}
	
	private void resize()
	{
		mData=new Integer[mSize.width][mSize.height];
	}

	public Vector<Object> getRow(int y) {
		Vector<Object> tmp = new  Vector<Object>();
		
		for(int x=0;x<mSize.width;x++)
			tmp.add(mData[x][y]);
			
		return tmp;
	}

	public Vector<Object> getData(List<Rectangle> areas) {
		Vector<Object> tmp=new Vector<Object>();
		Iterator<Rectangle> it=areas.iterator();
		while(it.hasNext())
		{
			Rectangle rct=it.next();
			tmp.addAll(getData(rct.x,rct.y,rct.x+rct.width-1,rct.y+rct.height-1));
		}
		return tmp;
	}

	public void setData(List<Rectangle> areas, List<Object> data) {
		Iterator<Rectangle> itr=areas.iterator();
		int from=0;
		int to;
		System.out.println("***SetData: AREAS:");
		System.out.println(areas.toString());
		System.out.println("***SetData: DATA:");
		System.out.println(data.toString());
		
		while(itr.hasNext())
		{
			Rectangle rct=itr.next();
			to=from+rct.width*rct.height; 
			this.setData(rct.x, rct.y, rct.x+rct.width-1,rct.y+rct.height-1, data.subList(from,to));
			from=to;
		}
		System.out.println("PO: "+getData().toString());
		
	}

	public void setData(int x1, int y1, int x2, int y2, List<Object> data) {
		if((x1+x2)>=mSize.width || (y1+y2)>=mSize.height) return;		

		Iterator<Object> it=data.iterator();
		for(int y=y1;y<=y2;y++)
			for(int x=x1;x<=x2;x++)
			{
				mData[x][y]=it.next();
			}
		
	}

	public CII_Tree_Vectors getInterface() {
		return mInterface;
	}

	public void setInterface(CII_Tree_Vectors iface) {
		mInterface=iface;
		mSize=new Dimension(Integer.parseInt(iface.get(5)),Integer.parseInt(iface.get(4)));	
		resize();
	}

	public void addTableModelListener(TableModelListener arg0) {
		super.addTableModelListener(arg0);
	}

	public Class<?> getColumnClass(int arg0) {
		return Object.class;
	}

	public int getColumnCount(){
		return mSize.width;
	}

	public String getColumnName(int columnIndex) {
		return new String(Integer.toString(columnIndex));
	}

	public int getRowCount() {
		return mSize.height;
	}

	public Object getValueAt(int row, int column) {
		return mData[column][row];
	}

	public boolean isCellEditable(int arg0, int arg1) {
		return true;
	}

	public void removeTableModelListener(TableModelListener arg0) {
		super.removeTableModelListener(arg0);
	}
	
	public void setValueAt(Object value, int row, int column) {
		Object old=mData[column][row];
		
		if(value instanceof String)
		{
			try{
				mData[column][row]=Integer.parseInt((String)value);
			} catch(Exception e)
			{
				mData[column][row]=null;
			}
		} else if(value instanceof Integer)
		{
			mData[column][row]=value;
		}
		if(old!=null && !((Integer)old).equals(mData[column][row]) || old==null && mData[column][row]!=null)
		{
			 fireTableCellUpdated(row, column);
		}
	}
}
